package com.akw.web.config;

import com.akw.web.bean.Person;
import com.akw.web.biz.UserBizHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

@Configuration
public class WebFunctionConfig {

    /**
     * 函数式Web:
     * 1. 给容器中放一个Bean: 类型是 RouterFunction<ServerResponse>
     * 2.RequestPredicate
     * 3.ServerRequest
     * 4. ServerResponse
     */
    @Bean
    public RouterFunction<ServerResponse> userRoute(UserBizHandler userBizHandler) {
        // 定义路由信息
        RouterFunctions.Builder route = RouterFunctions.route();
//        route.GET("/user/{id}", RequestPredicates.accept(MediaType.ALL), request -> {
//            // 业务处理
//            Person person = new Person(1L, "AKW", "2497744746@qq.com", 18);
//            // 构造响应
//            return ServerResponse.ok().body(person);
//        });
        route.GET("/user/{id}", RequestPredicates.accept(MediaType.ALL), userBizHandler::getUser);
        route.GET("/users", RequestPredicates.accept(MediaType.ALL), userBizHandler::list);
        route.POST("/user", RequestPredicates.accept(MediaType.APPLICATION_JSON), userBizHandler::addUser);
        route.PUT("/user", RequestPredicates.accept(MediaType.APPLICATION_JSON), userBizHandler::updateUser);
        route.DELETE("/user/{id}", RequestPredicates.accept(MediaType.ALL), userBizHandler::deleteUser);
        return route.build();
    }

}
